package ua.kharkov.khpi.tarasenko.bach.analysis.prefixspan;

import java.util.HashSet;
import java.util.Set;

import ua.kharkov.khpi.tarasenko.bach.core.logging.TransactionType;

public class Item implements Cloneable, Comparable<Item> {

	private String item;
	private TransactionType transactionType;
	private int support;

	public static Set<String> toSize = new HashSet<String>();
	
	private Item(String s) {
		setItem(s);
	}

	public static Item getItem(String s) {
		Item item = new Item(s.intern());
		toSize.add(s);
		return item;
	}

	@Override
	public String toString() {
		return getItem() + "(" + transactionType + "): " + support;
	}

	public boolean isEmpty() {
		if (getItem() != null) {
			return getItem().isEmpty();
		} else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((item == null) ? 0 : item.hashCode());
		result = prime * result
				+ ((transactionType == null) ? 0 : transactionType.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Item other = (Item) obj;
		if (item == null) {
			if (other.item != null)
				return false;
		} else if (!item.equals(other.item))
			return false;
		if (transactionType != other.transactionType)
			return false;
		return true;
	}

	public TransactionType getTransactionType() {
		return transactionType;
	}

	public void setTransactionType(TransactionType transactionType) {
		this.transactionType = transactionType;
	}

	public String getItem() {
		return item;
	}

	public void setItem(String item) {
		this.item = item;
	}

	public int getSupport() {
		return support;
	}

	public void setSupport(int support) {
		this.support = support;
	}

	@Override
	public int compareTo(Item o) {

		if (this.transactionType.ordinal() > o.transactionType.ordinal()) {
			return -1;
		} else if (this.transactionType.ordinal() < o.transactionType.ordinal()) {
			return 1;
		} else {

			if (this.getSupport() > o.getSupport()) {
				return -1;
			} else if (this.getSupport() < o.getSupport()) {
				return 1;
			} else {
				return this.item.compareTo(o.getItem());
			}
		}

	}
}
